package org.roma.ninemen;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;

/**
 * 
 * @author Mathias Larsson and Robin Weidelid
 *
 */
public class Brick {

	private final static int RED = 2;
	private final static int BLUE = 1;
	private double theta;
	private double hypotenuse;
	
	private Drawable image;
	private int color;
	private float xPos;
	private float yPos;
	private double xMove;
	private double yMove;
	private float xDestination;
	private float yDestination;
	private double speed = 5.0;
	private double tmpSpeed;
	private int position;
	
	public static final int SIZE = 20;
	
	public Brick(float xPos, float yPos) {
		this.xPos = xPos;
		this.yPos = yPos;
		xMove = 0;
		yMove = 0;
		xDestination = xPos;
		yDestination = yPos;
	}

	public Brick(Drawable image, float xPos, float yPos) {
		super();
		this.image = image;
		this.xPos = xPos;
		this.yPos = yPos;
		xDestination = xPos;
		yDestination = yPos;
	}

	public Brick(Drawable image, float xPos, float yPos, float xDestination, float yDestination) {
		super();
		this.image = image;
		this.xPos = xPos;
		this.yPos = yPos;
		this.xDestination = xDestination;
		this.yDestination = yDestination;
	}
	
	public Brick(Drawable image, int color, float xPos, float yPos, float xDestination, float yDestination) {
		super();
		this.image = image;
		this.color = color;
		this.xPos = xPos;
		this.yPos = yPos;
		this.xDestination = xDestination;
		this.yDestination = yDestination;
	}

	public void move() {
		
		if ( (int)xDestination != (int)xPos  && (int)yDestination != (int)yPos  ) {
			theta = Math.atan((Math.abs((yDestination - yPos)) / Math.abs((xDestination - xPos))));
			hypotenuse = Math.sqrt( Math.pow((yDestination - yPos), 2) + Math.pow((xDestination - xPos), 2) );
			
			if( hypotenuse < speed ){
				xMove = (Math.cos(theta) * hypotenuse);
				yMove = (Math.sin(theta) * hypotenuse);
			} else{
				xMove = (Math.cos(theta) * speed);
				yMove = (Math.sin(theta) * speed);
			}
			
			if(xDestination > xPos && yDestination > yPos){
				xPos += xMove;
				yPos += yMove;
			} 
			else if(xDestination > xPos && yDestination < yPos){
				xPos += xMove;
				yPos -= yMove;
			}
			else if(xDestination < xPos && yDestination > yPos){
				xPos -= xMove;
				yPos += yMove;
			}
			else if(xDestination < xPos && yDestination < yPos){
				xPos -= xMove;
				yPos -= yMove;
			}
		}
		else if( (int)xDestination == (int)xPos && (int)yDestination != (int)yPos ){
			tmpSpeed = speed;
			if( speed > Math.abs(yDestination - yPos)){
				tmpSpeed = Math.abs(yDestination - yPos);
			}
			
			if( yDestination < yPos ){
				yPos -= tmpSpeed;
			} else{
				yPos += tmpSpeed;
			}
		} 
		else if( (int)xDestination != (int)xPos && (int)yDestination == (int)yPos ){
			tmpSpeed = speed;
			if( speed > Math.abs(xDestination - xPos)){
				tmpSpeed = Math.abs(xDestination - xPos);
			}
			
			if( xDestination < xPos ){
				xPos -= tmpSpeed;
			} else{
				xPos += tmpSpeed;
			}
		} 
	}
	
	public void draw(Canvas canvas){
		move();
		image.setBounds((int)xPos, (int)yPos, (int)xPos+SIZE, (int)yPos+SIZE);
		image.draw(canvas);
	}
	
	
	public Drawable getImage() {
		return image;
	}

	public void setImage(Drawable image) {
		this.image = image;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public float getxPos() {
		return xPos;
	}

	public void setxPos(float xPos) {
		this.xPos = xPos;
	}

	public float getyPos() {
		return yPos;
	}

	public void setyPos(float yPos) {
		this.yPos = yPos;
	}

	public float getxDestination() {
		return xDestination;
	}

	public void setxDestination(float xDestination) {
		this.xDestination = xDestination;
	}

	public float getyDestination() {
		return yDestination;
	}

	public void setyDestination(float yDestination) {
		this.yDestination = yDestination;
	}
	
	public void setDestination(float x, float y){
		xDestination = x;
		yDestination = y;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}
	
	
	
}
